<template>
  <div class="centers">
    <div class="topindexmeny">
      <IndexMenu />
    </div>
    <div class="center">
      <div class="centerforms">
        <el-form
          :model="ruleFormwav"
          :rules="rules"
          ref="ruleFormwav"
          class="demo-ruleForm"
        >
          <el-form-item label="人物视频" v-loading="isvideos" prop="videoUrl">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleVideoSuccess"
              :before-upload="beforeVideoUpload"
              :data="{ projectId: videowavid, pptSort: 1 }"
              name="resource"
              accept=".mp4"
            >
              <video
                v-if="ruleFormwav.videoUrl"
                :src="url + ruleFormwav.videoUrl"
                width="100%"
                height="100%"
                autoplay
                controls
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="人物语音" prop="WavUrl" v-loading="isWavUrl">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleWavSuccess"
              :before-upload="beforeWavUpload"
              :data="{ projectId: videowavid, pptSort: 1 }"
              name="resource"
              accept=".wav,.mp3"
            >
              <audio
                v-if="ruleFormwav.WavUrl"
                :src="url + ruleFormwav.WavUrl"
                width="100%"
                height="100%"
                autoplay
                controls
                class="wav"
              ></audio>
              <i v-else class="el-icon-plus wav-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="画面质量">
            <el-radio-group v-model="ruleFormwav.level">
              <el-radio :label="1">流畅</el-radio>
              <el-radio :label="2">蓝光</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('ruleFormwav')">合 成</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="centerhinforms">
        <div class="centerhinformscharhone">
          <svg class="icon icontops" aria-hidden="true">
            <use xlink:href="#icon-a-duizhengquedagou"></use></svg
          >上传提示
        </div>
        <div class="centerhinformscharhtow">
          <ol>
            <li>1.视频与语音大小不能超过1G；</li>
            <li>2.视频的长度与宽度不能大于1600像素；</li>
            <li>3.视频帧率需是整数；</li>
            <li>4.视频只能上传格式为MP4的类型</li>
            <li>5.语音只能上传格式为WAV或MP3的类型</li>
          </ol>
        </div>
        <div class="centerhinformscharhthree">
          <div class="centerhinformscharhthreeone">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-zhuyi"></use></svg
            >注意事项
          </div>
          <div class="centerhinformscharhthreetow">
            <div class="projectindexform">
              <div class="projectindexformiconstyle">
                <svg class="icon projectindexformicon" aria-hidden="true">
                  <use xlink:href="#icon-baochianjing"></use>
                </svg>
              </div>
              <div class="projecttexticon">保持安静</div>
            </div>
            <div class="projectindexform">
              <div class="projectindexformiconstyle">
                <svg class="icon projectindexformicon" aria-hidden="true">
                  <use xlink:href="#icon-putonghua"></use>
                </svg>
              </div>
              <div class="projecttexticon">普通话发音标准</div>
            </div>
            <div class="projectindexform">
              <div class="projectindexformiconstyle">
                <svg class="icon projectindexformicon" aria-hidden="true">
                  <use xlink:href="#icon-luyinzhijian"></use>
                </svg>
              </div>
              <div class="projecttexticon">声音无杂音</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import IndexMenu from "@/components/menu/IndexMenu.vue";
import { FastCombine } from "@/api/projectwavendmp3";
export default {
  name: "ProWav",
  components: { IndexMenu },
  data() {
    return {
      url: this.$store.getters["url/urls"],
      isWavUrl: false,
      isvideos: false,
      uploadUrl:
        this.$store.getters["url/urls"] + "virtual/v1/userPro/resUpload",
      ruleFormwav: {
        videoUrl: "",
        WavUrl: "",
        level: 1,
      },
      rules: {
        videoUrl: [
          { required: true, message: "请选择人物视频", trigger: "change" },
        ],
        WavUrl: [{ required: true, message: "请选择语音", trigger: "change" }],
      },
      videowavid: this.$route.query.videoid - 0,
    };
  },
  mounted() {
    this.isvideowav();
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          FastCombine({
            projectId: this.videowavid,
            wavFile: this.ruleFormwav.WavUrl,
            videoFile: this.ruleFormwav.videoUrl,
            level:this.ruleFormwav.level
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success(res.msg);
              this.$router.push("/");
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //判断是否携带id传过来
    isvideowav() {
      if (!this.videowavid) {
        this.$message.error("非法跳转");
        this.$router.push("/");
      }
    },
    handleVideoSuccess(res, file) {
      if (res.code === 200) {
        this.$message.success(res.msg);
        // this.ruleFormwav.videoUrl = URL.createObjectURL(file.raw);
        this.ruleFormwav.videoUrl = res.data.path;
      } else {
        this.$message.error(res.msg);
      }
      this.isvideos = false;
    },
    beforeVideoUpload(file) {
      const isJPG = file.type === "video/mp4";
      const isLt1G = file.size / 1024 / 1024 / 1024 < 1;
      this.isvideos = true;

      if (!isJPG) {
        this.$message.error("上传人物视频只能是 MP4 格式!");
        this.isvideos = false;
      }
      if (!isLt1G) {
        this.$message.error("上传人物视频大小不能超过 1G!");
      }
      return isJPG && isLt1G;
    },
    beforeWavUpload(file) {
      this.isWavUrl = true;
      const isJPG = file.type === "audio/wav" || file.type === "audio/mpeg";
      const isLt1G = file.size / 1024 / 1024 / 1024 < 1;
      if (!isJPG) {
        this.$message.error("上传语音只能是 WAV或MP3 格式!");
        this.isWavUrl = false;
      }
      if (!isLt1G) {
        this.$message.error("上传语音大小不能超过 1G!");
      }
      return isJPG && isLt1G;
    },
    handleWavSuccess(res, file) {
      if (res.code === 200) {
        this.$message.success(res.msg);
        // this.ruleFormwav.WavUrl = URL.createObjectURL(file.raw);
        this.ruleFormwav.WavUrl = res.data.path;
      } else {
        this.$message.error(res.msg);
      }
      this.isWavUrl = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.centers {
  background-color: #f3f4f5;
  height: 100vh;
  box-sizing: border-box;
  .topindexmeny {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .center {
    height: 75vh;
    width: 90vw;
    margin: 2vh auto;
    background-color: #fff;
    padding: 4vh 5vw;
    box-sizing: border-box;
    .centerforms {
      float: left;
      width: 40%;
      height: 100%;
      .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 400px;
        height: 225px;
        line-height: 225px;
        text-align: center;
        border: 1px dashed #dcdfe6;
        background-color: #f5f7fa;
      }
      .avatar {
        width: 400px;
        height: 225px;
        display: block;
      }
      .wav {
        width: 400px;
        height: 100px;
        display: block;
      }
      .wav-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 400px;
        height: 100px;
        line-height: 100px;
        text-align: center;
        border: 1px dashed #dcdfe6;
        background-color: #f5f7fa;
      }
      .el-button {
        background-color: #434af9;
        color: #fff;
      }
    }
    .centerhinforms {
      float: left;
      text-align: left;
      background-color: #f5f7fa;
      padding: 30px;
      width: 40%;
      height: 55%;
      .centerhinformscharhone {
        color: #696f7a;
      }
      .centerhinformscharhtow {
        margin: 10px 0 20px 0;
        color: #9097a5;
        font-size: 14px;
        ol {
          list-style-type: none;
          line-height: 30px;
        }
      }
      .centerhinformscharhthree {
        .centerhinformscharhthreeone {
          color: #696f7a;
        }
        .centerhinformscharhthreetow {
          margin-top: 30px;
          .projectindexform {
            float: left;
            margin-right: 20px;
            .projectindexformiconstyle {
              width: 45px;
              height: 45px;
              margin: 0 auto;
              .projectindexformicon {
                width: 45px;
                height: 45px;
              }
            }
            .projecttexticon {
              line-height: 50px;
              font-size: 14px;
              color: #696f7a;
            }
          }
        }
      }
    }
  }
}
</style>